<template>
  <div class="pd-20">
    <el-select v-model="hospitalid"
               @change="getList"
               placeholder="请选择医院">
      <el-option v-for="item in medicalUnionList"
                 :key="item.id"
                 :label="item.hospitalName"
                 :value="item.id">
      </el-option>
    </el-select>
    <el-button type="primary"
               @click="getList">查询
    </el-button>
    <el-button type="primary"
               @click="handleOption('all')">全部设为网点
    </el-button>
    <element-table class="mt-20"
                   :columns="columns"
                   :data="tableData"
                   v-loading="loading">

      <template v-slot:option="{row}">
        <el-button type="primary"
                   v-if="row.isCdpSite==0"
                   @click="handleOption('setting', row)">设为网点
        </el-button>
        <el-button type="primary"
                   v-if="row.isCdpSite==1"
                   @click="handleOption('cancle', row)">取消网点
        </el-button>
      </template>
    </element-table>
  </div>
</template>

<script>
import ElementTable from '@/base/ElementTable'

export default {
  name: "index",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      hospitalid: '',
      medicalUnionList: [],
      loading: false,
      tableData: [],
      columns: [
        {
          label: '单位名称',
          prop: 'hospitalDto.name',
          align: "center",
          width: 200
        }, {
          label: '地址',
          prop: 'hospitalDto.address',
          align: "center"
        }, {
          label: '当前状态',
          prop: 'isCdpSite',
          align: "center",
          formatter: (row, cellValue) => {
            return row.isCdpSite == 1 ? '网点' : '非网点'
          },
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
          width: 200
        }
      ],

    }
  },
  created() {
    this.getMedicalUnionPage()
  },
  components: {
    'element-table': ElementTable
  },
  methods: {
    getList() {
      this.post(this.api.company.getMedicalUnionHospitalList, {
        healthCheckMedicalUnionId: this.hospitalid
      }).then((res) => {
        if (res.data.code === '200') {
          this.tableData = res.data.object
          this.pageTotal = res.data.totalNum
        }
      })
    },
    //获取医院列表
    getMedicalUnionPage() {
      this.post(this.api.company.getMedicalUnionPage, {
        leaderHospitalId: this.dataObj.hospitalId
      }).then((res) => {
        if (res.data.code === '200') {
          this.medicalUnionList = res.data.object
        }
      })
    },
    //更新网点
    updateMedicalUnionHospital(hospitalId, isCdpSite, isAllSetCdpSite) {
      this.post(this.api.company.updateMedicalUnionHospital, {
        id: hospitalId,
        healthCheckMedicalUnionId: this.hospitalid,
        isCdpSite: isCdpSite,
        isAllSetCdpSite: isAllSetCdpSite
      }).then((res) => {
        if (res.data.code === '200') {
          this.$message.success('操作成功')
          this.getList()
        }
      })
    },
    handleOption(op, row) {
      switch (op) {
        case 'select':
          this.getList()
          break
        case 'cancle':
          this.updateMedicalUnionHospital(row.id, 0, 0)
          break
        case 'setting':
          this.updateMedicalUnionHospital(row.id, 1, 0)
          break;
        case 'all':
          this.updateMedicalUnionHospital(null, null, 1)
          break

      }
    },


  }
}
</script>

<style scoped>

</style>
